//
//  XDynamicRecordTableViewCell.m
//  iAccountBook
//
//  Created by 侯伟 on 2021/9/21.
//

#import "XDynamicRecordTableViewCell.h"

@interface XDynamicRecordTableViewCell ()

@property(nonatomic,weak)UIImageView* typeImage;

@property(nonatomic,weak)UILabel* titleLb;

@property(nonatomic,weak)UILabel* contentLb;

@property(nonatomic,weak)UILabel* dateLb;

/// 显示内容视图
@property(nonatomic,weak) MyLinearLayout* contentLayout;

@end

@implementation XDynamicRecordTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        
        self.backgroundColor = UIColor.clearColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self initSubViews];
    }
    return self;
}

- (void)initSubViews{
    
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    contentLayout.padding = UIEdgeInsetsMake(0, 16,0, 16);
    contentLayout.myMargin = 0;
    contentLayout.myHeight = MyLayoutSize.wrap;
    contentLayout.subviewHSpace = 10;
    contentLayout.cacheEstimatedRect = YES;
    [self.contentView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    UIImageView* typeImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"x_dynamic_record"]];
    typeImage.myCenterY = typeImage.myLeft = 0;
//    typeImage.mySize = CGSizeMake(PX(50), PX(50));
    [contentLayout addSubview:typeImage];
    _typeImage = typeImage;
    
    MyLinearLayout *textLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    textLayout.backgroundColor = UIColor.whiteColor;
    textLayout.layer.cornerRadius = PX(16);
    textLayout.padding = UIEdgeInsetsMake(16, 16,16, 16);
    textLayout.myMargin = 0;
    textLayout.myHeight = MyLayoutSize.wrap;
    textLayout.cacheEstimatedRect = YES;
    textLayout.subviewVSpace = 8;
    textLayout.weight = 1.0;
    [contentLayout addSubview:textLayout];
    
    
    UILabel* titleLb = [UILabel lableWithFont:FONT_FIT(15) color:COLOR_BLACK_DARK text:@"--"];
    titleLb.myHorzMargin = titleLb.myTop = 0;
    [textLayout addSubview:titleLb];
    _titleLb = titleLb;

    UILabel* contentLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"--"];
    contentLb.myHorzMargin = 0;
    contentLb.myHeight = MyLayoutSize.wrap;
    contentLb.numberOfLines = 2;
    [textLayout addSubview:contentLb];
    _contentLb = contentLb;
    
    UILabel* timeLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_QUITE text:@"--"];
    [textLayout addSubview:timeLb];
    _dateLb = timeLb;
}


- (void)setDynamic:(XDynamicModel *)dynamic{
    
    _titleLb.text = dynamic.title;
    [_titleLb sizeToFit];

    _contentLb.text = dynamic.content;
    [_contentLb sizeToFit];

    _dateLb.text = [dynamic.date stringCutSeconds];
    [_dateLb sizeToFit];
}

#pragma private -- Size

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority{
    
    CGSize cellSize = CGSizeZero;
    if (@available(iOS 11.0, *)) {
        cellSize = [self.contentLayout sizeThatFits:CGSizeMake(targetSize.width - self.safeAreaInsets.left - self.safeAreaInsets.right, targetSize.height)];
    } else {
        CGSize size = targetSize;
        size.width = size.width;
        cellSize = [self.contentLayout sizeThatFits:size];  //如果使用系统自带的分割线，请记得将返回的高度height+1
    }
    return cellSize;
}
@end
